package com.sonic.ws.services.login;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.xml.ws.WebServiceContext;

import com.sonic.db.dao.MQLoginDAO;
import com.sonic.dto.ws.services.login.MQLoginRequest;
import com.sonic.dto.ws.services.login.MQLoginResponse;
import com.sonic.log.ejb.impl.MQLogger;
import com.sonic.ws.interfaces.login.IMQLoginService;

@WebService(name = "LoginService", serviceName = "LoginService", portName = "LoginServicePort", targetNamespace = "com.sonic.mquotes.login")
@SOAPBinding(style = SOAPBinding.Style.DOCUMENT, use = SOAPBinding.Use.LITERAL, parameterStyle = SOAPBinding.ParameterStyle.WRAPPED)
@Stateless(mappedName = "MQLoginServiceRemote")
@Remote(IMQLoginService.class)
@TransactionManagement(TransactionManagementType.CONTAINER)
@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
public class MQLoginService implements IMQLoginService {
	private static MQLogger logger = new MQLogger(MQLoginService.class, "MQ-WS-LOGIN");
	
	private MQLoginDAO mqLoginDAO;
	
	@SuppressWarnings("unused")
	@Resource
	private WebServiceContext context;
	
	@PostConstruct
	public void init() {
		final String methodName = "init()";
		logger.debug(methodName, " --- Successfully invoked init() method.");		
		this.mqLoginDAO = new MQLoginDAO();
		logger.debug(methodName, " --- MQLoginDAO Successfully created.");
	}
	
	@PreDestroy
	public void destroy() {
		final String methodName = "destroy()";
		logger.debug(methodName, " --- Successfully invoked destroy() method.");		
		this.mqLoginDAO.releaseResources();
		logger.debug(methodName, " --- MQLoginDAO resources have been successfully released.");
	}
	
	@WebMethod(operationName = "doLogin")
	public @WebResult(name = "loginResponse", targetNamespace = "com.sonic.mquotes.login")
	MQLoginResponse doLogin(@WebParam(name = "loginRequest", targetNamespace = "com.sonic.mquotes.login") MQLoginRequest request) {
		final String methodName = "doLogin";
		logger.debug(methodName, " --- Login user: " + request.getUserLogin());
		return this.mqLoginDAO.doAuthorization(request);
	}
}